//
//  HistoryScoreListViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/9/8.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "HistoryScoreListViewController.h"

#import "HistoryScoreTableViewCell.h"

@interface HistoryScoreListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    UITableView * _tableView;
    NSArray * _dataArray;
    int _page;
    int _totailPage;
    UIView * noBodyView;
}


@end

@implementation HistoryScoreListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"历史积分")
    
    [self getListData];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
        noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-150, 200, 200) andMessageStr:@"没有相关记录！" onView:nil];
    
    [self creatTableView];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getListData];
        
    }];
    _tableView.mj_header = header;
    
    [_tableView registerNib:[UINib nibWithNibName:[HistoryScoreTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[HistoryScoreTableViewCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    HistoryScoreTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[HistoryScoreTableViewCell reuseIdentifier] forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.starTime.text = dic[@"b_date"];
    cell.endTime.text = dic[@"e_date"];
    cell.scoreLabel.text = [NSString stringWithFormat:@"%@积分",dic[@"count"]];
    cell.statisLabel.text = dic[@"is_usable"];
    return cell;
    
}

-(void)getListData{
    
    
    NSDictionary * dic = @{
                           
                           @"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token")
                           };
    
        SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_closeScore_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
           
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                
                
                _dataArray = responseObject[@"datas"];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                    
                noBodyView.hidden =YES;
                    
                }else {
                    noBodyView.hidden =NO;
                    
                   [self.view addSubview:noBodyView];
                }
                [_tableView.mj_header endRefreshing];
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}


@end
